var form;
var tableIns;
var ctxPath = '/'
$(function () {
    layui.config({
        base: ctxPath + 'lib/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'//主入口模块
    }).use(['index', 'table', 'form'], function () {
        var table = layui.table;
        form = layui.form;
        // 渲染表格
        tableIns = table.render({
            elem: '#table',
            toolbar: '#table-title',
            url: 'sysCodePager',
            cols: [[
                {type: 'numbers'},
                {field: 'oi_name', title: '字典名', align: 'center'},
                {field: 'oi_value', title: '字典值', align: 'center'},
                {field: 'oi_code', title: '代码值', align: 'center'},
                {field: 'seq', title: '排序', align: 'center'},
                {field: 'right', title: '操作', toolbar: '#table-toolbar', align: 'center'},
            ]],
            where: {
                oi_name: $("#oiNameSearch").val()
            },
            page: true,
            request: {
                pageName: 'page', //页码的参数名称，默认：page
                limitName: 'limit' //每页数据量的参数名，默认：limit
            }
        });
        initOiNameSelect();
        //监听行工具事件
        table.on('tool(table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                openEdit(data)
            } else if (obj.event === 'del') {
                openDel(data);
            }
        });
        //监听提交
        form.on('submit(searchSubmit)', function () {
            tableIns.reload({
                where: {
                    oi_name: $("#oiNameSearch").val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
    });
})

function openEdit(data) {
    layer.open({
        type: 1,
        title: "编辑数据字典",
        fixed: false,
        resize: false,
        shadeClose: true,
        area: ['450px', '400px'],
        content: $('#codeEdit'),
        btn: ['确定', '取消'],
        success: function () {
            //填充表单
            form.val('codeForm', data);
            renderForm()
        }, btn1: function () {
            codeSubmit("1");
            tableReload();
        }, end: function () {
            cleanForm("codeForm")
        }
    });
}

function openAdd() {
    layer.open({
            type: 1,
            title: "新建数据字典",
            fixed: false,
            resize: false,
            shadeClose: true,
            area: ['450px', '400px'],
            content: $('#codeEdit'),
            btn: ['确定', '取消'],
            btn1: function () {
                codeSubmit("0");
                tableReload();
            },
            end: function () {
                cleanForm("codeForm")
            }
        }
    )
}

function codeSubmit(oper) {
    var type = oper == "0" ? "POST" : "PUT";
    $.ajax({
        type: type,
        data: $("#codeForm").serialize(),
        url: ctxPath + "config/syscode/sysCodeInfo"
    });
}

/**
 * 删除配置
 * @param obj
 */
function openDel(obj) {
    layer.confirm('您确定要删除配置：' + obj.option_params + '么', function () {
        $.ajax({
            type: "DELETE",
            data: {
                "id": obj.id
            },
            url: ctxPath + "config/syscode/sysCodeInfo",
            complete: function () {
                tableReload()
            }
        });
    });
}

function tableReload() {
    tableIns.reload({
        page: {
            curr: 1 //重新从第 1 页开始
        }
    });
}

function initOiNameSelect() {
    $.ajax({
        type: "GET",
        url: ctxPath + "config/syscode/oiNameList",
        success: function (res) {
            var data = res.obj;
            for (var i = 0; i < data.length; i++) {
                $("select[name$='oi_name']").append("<option value='" + data[i] + "'>" + data[i] + "</option>")
            }
            renderForm()
        }
    });
}